﻿#include <QCoreApplication>
#include"opencv.hpp"
using namespace cv;
using namespace std;

#include<iostream>

bool myfunction(int i, int j) {
  return (i>j);
}

#include<iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

Mat del_balck(Mat src1){
  //  cvtColor(src, dst, COLOR_BGR2GRAY);
    Mat dst = src1.clone();
   threshold(dst, dst, 0, 255, 0);
     Mat hline1 = getStructuringElement(MORPH_CROSS, Size(1, 7), Point(-1, -1));
     erode(dst, dst, hline1);
     Mat hline2 = getStructuringElement(MORPH_RECT, Size(13, 2), Point(-1, -1));
     dilate(dst, dst, hline2);

     std::vector<std::vector<cv::Point>> contours;
      std::vector<cv::Vec4i> hierarchy;
      Mat mask = dst.clone();
      if(mask.channels()!=1)
      cvtColor(mask,mask,COLOR_GRAY2BGR);
      Mat dst_mask = Mat::zeros(dst.rows,dst.cols,CV_8UC1);
    //只输出最顶层轮廓
    findContours(dst, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point());
    vector<cv::RotatedRect> ellipse_rect;
     vector<float> lens;
     vector<Rect> rect;
    for (int i = 0; i < contours.size(); i++)
    {

      if(contours[i].size() > 30)
      {
        RotatedRect current  = fitEllipse(contours[i]);

        Moments  mo = moments(contours[i], false);

        Point moCenter = Point2f(mo.m10 / mo.m00, mo.m01 / mo.m00);

        if(moCenter.x>dst.cols/2)continue;
         cv::Scalar color;
                 color = cv::Scalar(50,128,200);
         float w = current.size.width;
         float h = current.size.height;
         float len = max(w,h);
         lens.push_back(len);
          if(abs(current.angle)<90&&(abs(90-current.angle)>5)){
            if (abs(current.angle)>5){
              cout<<current.angle;
              continue;
            }
          }
          ellipse_rect.push_back(current);
         drawContours(mask, contours, i,cv::Scalar(0,128,200), 1, 8);
         drawContours(dst_mask, contours, i,cv::Scalar(255,128,200), 1, 8);
         rect.push_back(boundingRect(contours[i]));
        floodFill(dst_mask,moCenter,cv::Scalar(255,128,200));
      }
    }
        int rect_h =0;
        for(int i =0;i<rect.size();i++){
            Rect rect1 = rect[i];

            rect_h += rect1.height;

        }
        rect_h = rect_h/rect.size()+3;
        for(int i =0;i<rect.size();i++){
            Rect rect1 = rect[i];
            rect1.x=0;
            rect1.width = dst.cols-1;
            rect1.height = rect_h;
            rectangle(dst_mask, rect1, Scalar(255, 0, 0),-1,8,0);
        }
       ;
       Mat hline111 = getStructuringElement(MORPH_CROSS, Size(1, 7), Point(-1, -1));

       dilate(dst_mask, dst_mask, hline111);
       threshold(dst_mask, dst_mask, 200, 255, THRESH_BINARY);

    return dst_mask;

}
Mat up(Mat src1){

  Mat src, dst,temp;
  src = src1.clone();

  if (src.empty())
  {
    cout << "could not load image..." << endl;
//    return -1;
  }

//  src = src(Rect(Point(0,src.rows/2),Point(src.cols,src.rows)));
  src = src(Rect(Point(0,0),Point(src.cols,src.rows/2)));
  Mat crop = src.clone();
  if(crop.channels()!=1)
  cvtColor(crop, crop, COLOR_BGR2GRAY);
//  namedWindow("up",WINDOW_FREERATIO);
//  imshow("up", src);
  temp = src.clone();
  if(temp.channels()!=1)
  cvtColor(src, src, COLOR_BGR2GRAY);

  equalizeHist(src, src);
      normalize(src,src,0,1.0f,NORM_MINMAX,CV_32FC1);
    addWeighted(src,3,src,0,0,src);
    normalize(src,src,0,255,NORM_MINMAX,CV_8UC1);

  threshold(src, src, 35, 255, THRESH_TOZERO_INV);
//  namedWindow("up_threshold1",WINDOW_FREERATIO);
//  imshow("up_threshold1", src);

  Mat a1;
  threshold(src, a1, 15, 255, 0);
//  namedWindow("up_threshold2",WINDOW_FREERATIO);
//  imshow("up_threshold2", a1);
  //输入为35以下的
  Mat black_mask = del_balck(src);
//  namedWindow("black",WINDOW_FREERATIO);
//  imshow("black", black_mask);
  Mat add1;

  Mat th_black_mask = a1 - black_mask;
//  namedWindow("a1 - black_mask",WINDOW_FREERATIO);
//  imshow("a1 - black_mask", th_black_mask);

  Mat hline111 = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
  dilate(th_black_mask, th_black_mask, hline111);
  Mat hline222 = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
  erode(th_black_mask, th_black_mask, hline222);


  namedWindow("th_black_mask1",WINDOW_FREERATIO);
  imshow("th_black_mask1", th_black_mask);

  return th_black_mask;
}
